package com.homedo.odin.solution.controller.mng;

import com.homedo.core.common.base.controller.BaseController;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.core.common.exception.BizException;
import com.homedo.odin.solution.contract.mng.BrandEntranceListReq;
import com.homedo.odin.solution.contract.mng.BrandEntranceListResp;
import com.homedo.odin.solution.contract.mng.BrandFirmsEntranceResp;
import com.homedo.odin.solution.contract.mng.BrandOwnEditReq;
import com.homedo.odin.solution.contract.oms.BrandOwnerReq;
import com.homedo.odin.solution.enumcode.basic.DbSortEnum;
import com.homedo.odin.solution.model.request.pc.brand.BrandListRequestBean;
import com.homedo.odin.solution.model.response.pc.brand.BrandOwnerDetailResponse;
import com.homedo.odin.solution.service.BrandOwnerServiceImpl;
import com.homedo.odin.solution.service.bizservice.mng.MngBrandBizService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.validation.Valid;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: donglibing
 * @date: 2018/3/27 17:30
 * @Description:
 */
@Api(description = "品牌", tags = {"Brand"}, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/mng/brand")
public class MngBrandController extends BaseController<MngBrandBizService> {

    @Autowired
    private BrandOwnerServiceImpl brandOwnerService;

    @ApiOperation(value = "OMS_获取品牌列表", notes = "OMS_获取品牌列表", response = BrandOwnerDetailResponse.class, httpMethod = "GET")
    @GetMapping()
    public ResultData getBrands(@ModelAttribute BrandListRequestBean requestBean) {
        return ResultData.newInstance(brandOwnerService.getBranOwners(requestBean));
    }

    @ApiOperation(value = "OMS_获取品牌列表(大牌原厂入口管理)分页", notes = "OMS_获取品牌列表(大牌原厂入口管理)分页",
            response = BrandEntranceListResp.class, httpMethod = "GET")
    @GetMapping("/brand_firms")
    public ResultData getBrandEntrancePage(@ModelAttribute("req") BrandEntranceListReq req,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return ResultData.newInstance(Meta.CODE_451, defaultMessage);
        }
        if (StringUtils.isNotBlank(req.getSort())) {
            if (!DbSortEnum.dbValueExist(req.getSort().toUpperCase())) {
                throw new BizException(Meta.CODE_451.getCode(), "排序字段异常");
            }
        }
        return ResultData.newInstance(baseService.queryBrandEntranceList(req));
    }

    @ApiOperation(value = "OMS_新增品牌", notes = "OMS_新增品牌", httpMethod = "POST")
    @PostMapping()
    public ResultData addBrands(@RequestBody BrandOwnerReq req) {
        baseService.addorUpdateBrandOwner(req, 0);
        return ResultData.newInstance(Meta.CODE_200, "新增品牌成功");
    }

    @ApiOperation(value = "OMS_修改品牌", notes = "OMS_修改品牌", httpMethod = "PUT")
    @PutMapping("/{brandId}")
    public ResultData updateBrands(@RequestBody BrandOwnerReq req,
            @ApiParam(value = "品牌ID", required = true) @PathVariable("brandId") int brandId) {
        baseService.addorUpdateBrandOwner(req, brandId);
        return ResultData.newInstance(Meta.CODE_200, "修改品牌成功");
    }

    @ApiOperation(value = "OMS_查看品牌详情", notes = "OMS_查看品牌详情", httpMethod = "GET")
    @GetMapping("/{brandId}")
    public ResultData brandInfo(@ApiParam(value = "品牌ID", required = true) @PathVariable("brandId") int brandId) {
        return ResultData.newInstance(baseService.queryBrandOwnerDetail(brandId));
    }

    @ApiOperation(value = "OMS_获取品牌列表(大牌原厂入口管理,显示的品牌都会拥有至少一个品牌厂商,大牌原厂入口管理 悬浮窗查询)", response = BrandFirmsEntranceResp.class, httpMethod = "GET")
    @GetMapping("/entrance")
    public ResultData getBrandFirmsEntrance() {
        BrandFirmsEntranceResp resp = baseService.queryBrandFirmsEntrance();
        return ResultData.newInstance(Meta.CODE_200, resp);
    }

    @ApiOperation(value = "OMS_修改品牌序号（前台运营大牌原厂入口管理）", notes = "OMS_修改品牌序号（前台运营大牌原厂入口管理）", httpMethod = "PUT")
    @PutMapping("/entrance")
    public ResultData editBrandSort(@Valid @RequestBody BrandOwnEditReq brandOwnEditReq, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return ResultData.newInstance(Meta.CODE_451, defaultMessage);
        }
        baseService.editBrandSort(brandOwnEditReq);
        return ResultData.newInstance(Meta.CODE_200, "更新序号成功");
    }

}
